package com.android.kotlincoroutinemvvmdemo.view

import android.animation.Animator
import android.animation.ObjectAnimator
import android.animation.ValueAnimator
import android.content.Context
import android.graphics.Canvas
import android.graphics.Paint
import android.graphics.RectF
import android.util.AttributeSet
import android.view.View
import android.view.animation.LinearInterpolator
import com.android.kotlincoroutinemvvmdemo.R
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Job
import kotlinx.coroutines.MainScope
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.launch

class RectProgressRotateView @JvmOverloads constructor(
    context: Context,
    attributeSet: AttributeSet? = null
) : View(context,attributeSet), CoroutineScope by MainScope() {

    private var mWidth = 0
    private var mHeight = 0

    private var startAngle : Float = 0F
        set(value) {
            field = value
            invalidate()
        }

    private var job: Job? = null

    private val angleArray = floatArrayOf(0F, 45F, 90F, 135F, 180F, 225F, 270F, 315F)

    private val mPaint0 by lazy {
        Paint().apply {
            isAntiAlias = true
            isDither = true
            color = context.resources.getColor(R.color.color_da8989)
            style = Paint.Style.FILL
        }
    }

    private val mPaint1 by lazy {
        Paint().apply {
            isAntiAlias = true
            isDither = true
            color = context.resources.getColor(R.color.color_64C630)
            style = Paint.Style.FILL
        }
    }

    private val mPaint2 by lazy {
        Paint().apply {
            isAntiAlias = true
            isDither = true
            color = context.resources.getColor(R.color.color_B128C9)
            style = Paint.Style.FILL
        }
    }

    private val mPaint3 by lazy {
        Paint().apply {
            isAntiAlias = true
            isDither = true
            color = context.resources.getColor(R.color.color_ff5722)
            style = Paint.Style.FILL
        }
    }

    private val mPaint4 by lazy {
        Paint().apply {
            isAntiAlias = true
            isDither = true
            color = context.resources.getColor(R.color.teal_200)
            style = Paint.Style.FILL
        }
    }

    private val mPaint5 by lazy {
        Paint().apply {
            isAntiAlias = true
            isDither = true
            color = context.resources.getColor(R.color.color_ffeb3b)
            style = Paint.Style.FILL
        }
    }

    private val mPaint6 by lazy {
        Paint().apply {
            isAntiAlias = true
            isDither = true
            color = context.resources.getColor(R.color.color_aa6200ee)
            style = Paint.Style.FILL
        }
    }

    private val mPaint7 by lazy {
        Paint().apply {
            isAntiAlias = true
            isDither = true
            color = context.resources.getColor(R.color.color_FDBB8FFF)
            style = Paint.Style.FILL
        }
    }

    private val mPaintArray =
        arrayOf(mPaint0, mPaint1, mPaint2, mPaint3, mPaint4, mPaint5, mPaint6, mPaint7)

    private val rectF by lazy {
        RectF()
    }

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)
        mWidth = when(MeasureSpec.getMode(widthMeasureSpec)) {
            MeasureSpec.EXACTLY -> {
                MeasureSpec.getSize(widthMeasureSpec)
            }
            else -> {
                context.resources.getDimension(R.dimen.dp_100).toInt()
            }
        } - paddingStart - paddingEnd

        mHeight = when(MeasureSpec.getMode(heightMeasureSpec)) {
            MeasureSpec.EXACTLY -> {
                MeasureSpec.getSize(heightMeasureSpec)
            }
            else -> {
                context.resources.getDimension(R.dimen.dp_100).toInt()
            }
        } - paddingTop - paddingBottom

        setMeasuredDimension(mWidth, mHeight)
    }

    override fun onDraw(canvas: Canvas?) {
        super.onDraw(canvas)
        canvas?.apply {
            setBackgroundColor(resources.getColor(R.color.color_F3F3F3))

            translate(mWidth / 2.0F, mHeight / 2.0F)

            for (i in mPaintArray.indices) {
                canvas.save()
                rotate(angleArray[i] + startAngle)
                rectF.set(
                    10F + 25F,
                    -10F,
                    60F + 25F,
                    10F
                )
                drawRect(rectF, mPaintArray[i])
                canvas.restore()
            }

//            canvas.save()
//            rotate(0F + startAngle)
//            rectF.set(
//                10F + 25F,
//                -10F,
//                60F + 25F,
//                10F)
//            drawRect(rectF, mPaint0)
//            canvas.restore()
//
//            canvas.save()
//            rotate(45F + startAngle)
//            rectF.set(
//                10F + 25F,
//                -10F,
//                60F + 25F,
//                10F)
//            drawRect(rectF, mPaint1)
//            canvas.restore()
//
//            canvas.save()
//            rotate(90F + startAngle)
//            rectF.set(
//                10F + 25F,
//                -10F,
//                60F + 25F,
//                10F)
//            drawRect(rectF, mPaint2)
//            canvas.restore()
//
//            canvas.save()
//            rotate(135F + startAngle)
//            rectF.set(
//                10F + 25F,
//                -10F,
//                60F + 25F,
//                10F)
//            drawRect(rectF, mPaint3)
//            canvas.restore()
//
//            canvas.save()
//            rotate(180F  + startAngle)
//            rectF.set(
//                10F + 25F,
//                -10F,
//                60F + 25F,
//                10F)
//            drawRect(rectF, mPaint4)
//            canvas.restore()
//
//            canvas.save()
//            rotate(225F + startAngle)
//            rectF.set(
//                10F + 25F,
//                -10F,
//                60F + 25F,
//                10F)
//            drawRect(rectF, mPaint5)
//            canvas.restore()
//
//            canvas.save()
//            rotate(270F + startAngle)
//            rectF.set(
//                10F + 25F,
//                -10F,
//                60F + 25F,
//                10F)
//            drawRect(rectF, mPaint6)
//            canvas.restore()
//
//            canvas.save()
//            rotate(315F + startAngle)
//            rectF.set(
//                10F + 25F,
//                -10F,
//                60F + 25F,
//                10F)
//            drawRect(rectF, mPaint7)
//            canvas.restore()
        }
    }


    fun startAnimation() {
//        val objectAnimator =
//            ObjectAnimator.ofFloat(this, "rotation", 0F, 360F)
//        objectAnimator.addListener(object : Animator.AnimatorListener {
//            override fun onAnimationStart(animation: Animator) {}
//            override fun onAnimationCancel(animation: Animator) {}
//            override fun onAnimationRepeat(animation: Animator) {}
//            override fun onAnimationEnd(animation: Animator) {
//                //Log.d(TAG,"---onAnimationEnd---")
//                //mStartAngle += floatArray[i]
//            }
//        })
//        objectAnimator.duration = 1000
//        //使用差值器避免中间会出现的停顿
//        objectAnimator.interpolator = LinearInterpolator()
//        objectAnimator.repeatMode = ValueAnimator.RESTART
//        objectAnimator.repeatCount = ValueAnimator.INFINITE
//        objectAnimator.start()
        job = launch {
            flow {
                while (true) {
                    emit(0)
                    delay(150)
                }
            }.collect {
                startAngle += 45F
                startAngle %= 360F
                invalidate()
            }
        }
        job?.start()
    }

    fun stopAnimation() {
        job?.cancel()
    }


    companion object {
        const val TAG = "AnimationView"
    }
}